//
//  NSObject+Convert.m
//  xdmall
//
//  Created by kane on 15/9/2.
//  Copyright (c) 2015年 朱建宇. All rights reserved.
//

#import "NSObject+Convert.h"

@implementation NSObject(Convert)

- (NSString *)convertToString
{
    if (self == nil || [self isEqual:[NSNull null]] || [self isEqual:@"null"]) {
        return @"";
    }
    return [NSString stringWithFormat:@"%@",self];
}

@end

@implementation UIColor (extension)

+ (UIColor*)colorWithHex:(NSInteger)hexValue alpha:(CGFloat)alphaValue
{
    return [UIColor colorWithRed:((float)((hexValue & 0xFF0000) >> 16))/255.0
                           green:((float)((hexValue & 0xFF00) >> 8))/255.0
                            blue:((float)(hexValue & 0xFF))/255.0 alpha:alphaValue];
}

+ (UIColor*) colorWithHex:(NSInteger)hexValue
{
    return [UIColor colorWithHex:hexValue alpha:1.0];
}

+ (NSString *) hexFromUIColor: (UIColor*) color {
    if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        color = [UIColor colorWithRed:components[0]
                                green:components[0]
                                 blue:components[0]
                                alpha:components[1]];
    }
    
    if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
        return [NSString stringWithFormat:@"#FFFFFF"];
    }
    
    return [NSString stringWithFormat:@"#%x%x%x", (int)((CGColorGetComponents(color.CGColor))[0]*255.0),
            (int)((CGColorGetComponents(color.CGColor))[1]*255.0),
            (int)((CGColorGetComponents(color.CGColor))[2]*255.0)];
}

@end


@implementation UIFont (category)

+ (UIFont*)fontSize:(NSInteger)size
{
    switch (FONT_SIZE) {
        case -1:
            return [UIFont systemFontOfSize:size - 2];
        case 1:
            return [UIFont systemFontOfSize:size + 2];
        case 2:
            return [UIFont systemFontOfSize:size + 4];
        default:
            return [UIFont systemFontOfSize:size];
    }
}

+ (UIFont*)boldFontSize:(NSInteger)size
{
    switch (FONT_SIZE) {
        case -1:
            return [UIFont boldSystemFontOfSize:size - 2];
        case 1:
            return [UIFont boldSystemFontOfSize:size + 2];
        case 2:
            return [UIFont boldSystemFontOfSize:size + 4];
        default:
            return [UIFont boldSystemFontOfSize:size];
    }
}

@end